home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / hity wydania / Ubuntu 9.10 PL / karmelkowy-koliberek-9.10-netbook-remix-PL.iso / casper / filesystem.squashfs / usr / share / hplip / info.py < prev    next >
Text File  |  2009-10-09  |  6KB  |  187 lines

  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. #
  4. # (c) Copyright 2003-2009 Hewlett-Packard Development Company, L.P.
  5. #
  6. # This program is free software; you can redistribute it and/or modify
  7. # it under the terms of the GNU General Public License as published by
  8. # the Free Software Foundation; either version 2 of the License, or
  9. # (at your option) any later version.
  10. #
  11. # This program is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License
  17. # along with this program; if not, write to the Free Software
  18. # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
  19. #
  20. # Author: Don Welch
  21. #
  22.  
  23. __version__ = '5.2'
  24. __title__ = 'Device Information Utility'
  25. __mod__ = 'hp-info'
  26. __doc__ = "Query a printer for static model information, dynamic status information, and CUPS printer queue settings."
  27.  
  28. # Std Lib
  29. import sys
  30. import getopt
  31. import time
  32. import os
  33.  
  34. # Local
  35. from base.g import *
  36. from base import device, status, utils, tui, module
  37. from prnt import cups
  38.  
  39. try:
  40.     restrict = True
  41.  
  42.     devid_mode = '--id' in sys.argv # hack
  43.     if devid_mode:
  44.         log.set_level("none")
  45.         restrict = False
  46.  
  47.     mod = module.Module(__mod__, __title__, __version__, __doc__, None,
  48.                         (INTERACTIVE_MODE, GUI_MODE), (UI_TOOLKIT_QT4,),
  49.                         False, devid_mode)
  50.  
  51.     mod.setUsage(module.USAGE_FLAG_DEVICE_ARGS,
  52.         extra_options=[("Device ID mode:", "--id (prints device ID only and exits.) (interactive (-i) mode only.)", "option", False),
  53.                        ("Allow device URIs of uninstalled printers:", "-x (interactive (-i) mode only.)", "option", False)
  54.                         ],
  55.          see_also_list=['hp-toolbox'])
  56.  
  57.     opts, device_uri, printer_name, mode, ui_toolkit, lang = \
  58.         mod.parseStdOpts('x', ['id'])
  59.  
  60.  
  61.  
  62.     for o, a in opts:
  63.         if o == '-x':
  64.             restrict = False
  65.  
  66.     if devid_mode:
  67.         mode = INTERACTIVE_MODE
  68.  
  69.     if mode == GUI_MODE:
  70.         if not utils.canEnterGUIMode4():
  71.             log.error("%s -u/--gui requires Qt4 GUI support. Entering interactive mode." % __mod__)
  72.             mode = INTERACTIVE_MODE
  73.  
  74.     if mode == GUI_MODE:
  75.         restrict = True
  76.  
  77.     device_uri = mod.getDeviceUri(device_uri, printer_name, restrict_to_installed_devices=restrict)
  78.  
  79.     if mode == INTERACTIVE_MODE:
  80.         try:
  81.             d = device.Device(device_uri, printer_name)
  82.         except Error:
  83.             log.error("Unexpected error. Exiting.")
  84.             sys.exit(1)
  85.  
  86.         if not devid_mode:
  87.             log.info("")
  88.             log.info(log.bold(d.device_uri))
  89.             log.info("")
  90.  
  91.         try:
  92.             try:
  93.                 d.open()
  94.                 d.queryDevice()
  95.             except Error, e:
  96.                 log.error("Error opening device (%s)." % e.msg)
  97.                 #sys.exit(1)
  98.  
  99.             if not devid_mode:
  100.                 formatter = utils.TextFormatter(
  101.                                 (
  102.                                     {'width': 28, 'margin' : 2},
  103.                                     {'width': 58, 'margin' : 2},
  104.                                 )
  105.                             )
  106.  
  107.             if devid_mode:
  108.                 try:
  109.                     if d.dq['deviceid']:
  110.                         print(d.dq['deviceid'])
  111.                     sys.exit(0)
  112.                 except KeyError:
  113.                     log.error("Device ID not available.")
  114.             else:
  115.                 dq_keys = d.dq.keys()
  116.                 dq_keys.sort()
  117.  
  118.                 log.info(log.bold("Device Parameters (dynamic data):"))
  119.                 log.info(log.bold(formatter.compose(("Parameter", "Value(s)"))))
  120.                 log.info(formatter.compose(('-'*28, '-'*58)))
  121.  
  122.                 for key in dq_keys:
  123.                     log.info(formatter.compose((key, str(d.dq[key]))))
  124.  
  125.                 log.info(log.bold("\nModel Parameters (static data):"))
  126.                 log.info(log.bold(formatter.compose(("Parameter", "Value(s)"))))
  127.                 log.info(formatter.compose(('-'*28, '-'*58)))
  128.  
  129.                 mq_keys = d.mq.keys()
  130.                 mq_keys.sort()
  131.  
  132.                 for key in mq_keys:
  133.                     log.info(formatter.compose((key, str(d.mq[key]))))
  134.  
  135.                 if d.dbus_avail:
  136.                     formatter = utils.TextFormatter(
  137.                                     (
  138.                                         {'width': 20, 'margin' : 2}, # date/time
  139.                                         {'width': 5, 'margin' : 2}, # code
  140.                                         {'width': 40, 'margin' : 2}, # desc
  141.                                         {'width': 8, 'margin' : 2}, # user
  142.                                         {'width': 8, 'margin' : 2}, # job id
  143.                                     )
  144.                                 )
  145.  
  146.                     log.info(log.bold("\nStatus History (most recent first):"))
  147.                     log.info(log.bold(formatter.compose(("Date/Time", "Code", "Status Description", "User", "Job ID"))))
  148.                     log.info(formatter.compose(('-'*20, '-'*5, '-'*40, '-'*8, '-'*8)))
  149.  
  150.                     hq = d.queryHistory()
  151.  
  152.                     for h in hq:
  153.                         desc = device.queryString(h.event_code)
  154.                         log.info(formatter.compose((time.strftime("%x %H:%M:%S", time.localtime(h.timedate)),
  155.                             str(h.event_code), desc, h.username, str(h.job_id))))
  156.  
  157.                     log.info("")
  158.         finally:
  159.             d.close()
  160.  
  161.     else: # GUI mode
  162.         try:
  163.             from PyQt4.QtGui import QApplication
  164.             from ui4.infodialog import InfoDialog
  165.         except ImportError:
  166.             log.error("Unable to load Qt4 support. Is it installed?")
  167.             sys.exit(1)
  168.  
  169.         if 1:
  170.             app = QApplication(sys.argv)
  171.  
  172.             dlg = InfoDialog(None, device_uri)
  173.             dlg.show()
  174.             try:
  175.                 log.debug("Starting GUI loop...")
  176.                 app.exec_()
  177.             except KeyboardInterrupt:
  178.                 sys.exit(0)
  179.  
  180.  
  181. except KeyboardInterrupt:
  182.     log.error("User exit")
  183.  
  184. log.info("")
  185. log.info("Done.")
  186.  
  187.